package com.itany.jdbc;

import jdk.nashorn.internal.scripts.JD;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 * Author:shixiaojun@itany.com
 * Date:2022/11/11 11:15
 * Version:1.0-SNAPSHOT
 * Description:
 */
public class Test05 {

    public static void main(String[] args) {
//        Dept dept4 = new Dept("aa","Boss");
//        Dept dept = new Dept("bb","研发产品");
//        Dept dept2 = new Dept("cc","开拓市场");
//        Dept dept3 = new Dept("dd","管钱的");
//        insertDept(dept4);
//        insertDept(dept);
//        insertDept(dept2);
//        insertDept(dept3);

//        Dept dept = new Dept(4,"修改部","我改了");
//        updateDept(dept);
//        deleteById(8);
//        List<Dept> depts = selectAll();
        List<Dept> depts = selectPage(2,5);
        for(Dept dept : depts){
            System.out.println(dept);
        }
    }

    public static void insertDept(Dept dept){
        Connection conn = null;
        PreparedStatement ps = null;
        try {
            conn = JDBCUtil.getConnection();
            String sql = new StringBuffer()
                    .append(" insert into ")
                    .append(" t_dept ")
                    .append("   (name,remark) ")
                    .append(" values ")
                    .append("   (?,?)")
                    .toString();
            ps = conn.prepareStatement(sql);
            ps.setString(1,dept.getName());
            ps.setString(2,dept.getRemark());

            ps.executeUpdate();
            System.out.println("添加成功");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.close(conn,ps,null);
        }
    }

    public static void deleteById(Integer id){
        Connection conn = null;
        PreparedStatement ps = null;
        try {
            conn = JDBCUtil.getConnection();
            String sql = new StringBuffer()
                   .append(" delete from t_dept")
                    .append(" where id = ? ")
                    .toString();
            ps = conn.prepareStatement(sql);
            ps.setInt(1,id);
            ps.executeUpdate();
            System.out.println("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.close(conn,ps,null);
        }
    }

    public static void updateDept(Dept dept){
        Connection conn = null;
        PreparedStatement ps = null;
        try {
            conn = JDBCUtil.getConnection();
            String sql = new StringBuffer()
                    .append(" update t_dept ")
                    .append(" set ")
                    .append("   name = ?, ")
                    .append("   remark = ? ")
                    .append(" where id = ? ")
                    .toString();
            ps = conn.prepareStatement(sql);
            ps.setString(1,dept.getName());
            ps.setString(2,dept.getRemark());
            ps.setInt(3,dept.getId());
            ps.executeUpdate();
            System.out.println("修改成功");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.close(conn,ps,null);
        }
    }

    public static List<Dept> selectAll(){
        List<Dept> depts = new ArrayList<>();
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            conn = JDBCUtil.getConnection();
            String sql = new StringBuffer()
                    .append(" select id,name,remark ")
                    .append(" from t_dept ")
                    .toString();
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            while (rs.next()){
                Dept dept = new Dept();
                dept.setId(rs.getInt("id"));
                dept.setName(rs.getString("name"));
                dept.setRemark(rs.getString("remark"));
                depts.add(dept);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.close(conn,ps,rs);
        }
        return depts;
    }

    public static List<Dept> selectPage(Integer pageNo,Integer pageSize){
        List<Dept> depts = new ArrayList<>();
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            conn = JDBCUtil.getConnection();
            String sql = new StringBuffer()
                    .append(" select id,name,remark ")
                    .append(" from t_dept ")
                    .append(" limit ?,?")
                    .toString();
            ps = conn.prepareStatement(sql);
            ps.setInt(1,(pageNo-1)*pageSize);
            ps.setInt(2,pageSize);
            rs = ps.executeQuery();
            while (rs.next()){
                Dept dept = new Dept();
                dept.setId(rs.getInt("id"));
                dept.setName(rs.getString("name"));
                dept.setRemark(rs.getString("remark"));
                depts.add(dept);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.close(conn,ps,rs);
        }
        return depts;
    }

}
